package com.drug.mingkang.controller;

import cn.hutool.http.HtmlUtil;
import com.drug.mingkang.model.HqPurchaseorder;
import com.drug.mingkang.model.HqSupplier;
import com.drug.mingkang.model.vo.PurchaseMaterialVo;
import com.drug.mingkang.model.vo.PurchaseorderVo;
import com.drug.mingkang.model.vo.rawMaterialVo;
import com.drug.mingkang.service.HqPurchaseorderService;
import com.drug.mingkang.utils.AuditStatus;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 原材料采购单(HqPurchaseorder)表控制层
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:13
 */
@RestController
@RequestMapping("hqPurchaseorder")
@Api(value = "原材料采购单控制层", tags = "原材料采购单Contorller")
public class HqPurchaseorderController {
    /**
     * 服务对象
     */
    @Resource
    private HqPurchaseorderService hqPurchaseorderService;
    @Resource
    private Tools tools;

    /**
     * 原材料采购页面显示全部
     * @param vo 原材料采购单参数
     * @return 原材料采购单列表
     */
    @ApiOperation(value = "原材料采购页面显示全部", notes = "test|page:1,limit:10", httpMethod = "POST", response = Map.class)
    @PostMapping("showAll")
    public Map<String, Object> showAll(PurchaseorderVo vo) {
        List<HqPurchaseorder> list = hqPurchaseorderService.queryAllByLimit(vo);
        int count = hqPurchaseorderService.selectCount(vo);
        return count < 0 ? tools.limit(null, count) : tools.limit(list, count);
    }

    /**
     * 原材料增加页面 显示全部原材料信息
     * @param vo 原材料参数
     * @return 原材料列表
     */
    @ApiOperation(value = "原材料增加页面 显示全部原材料信息", notes = "test|page:1,limit:10", httpMethod = "POST", response = Map.class)
    @PostMapping("rawMaterialShowAll")
    public Map<String, Object> rawMaterialShowAll(rawMaterialVo vo) {
        List<rawMaterialVo> list = hqPurchaseorderService.rawMaterialShowAllByLimit(vo);
        int count = hqPurchaseorderService.rawMaterialselectCount(vo);
        return tools.limit(list, count);
    }

    /**
     * 查询所有原材料分类名称
     * @return 原材料分类列表
     */
    @ApiOperation(value = "查询所有原材料分类名称", notes = "", httpMethod = "POST", response = List.class)
    @PostMapping("showTypeName")
    public List<Object> showTypeName() {
        return hqPurchaseorderService.showTypeName();
    }

    /**
     * 查询所有有效的供应商名称
     * @return 供应商列表
     */
    @ApiOperation(value = "查询所有有效的供应商名称", notes = "", httpMethod = "POST", response = List.class)
    @PostMapping("showSupName")
    public List<HqSupplier> showSupName() {
        return hqPurchaseorderService.showSupName();
    }

    /**
     * 原材料增加
     * @param arr 原材料集合json
     * @param remark 采购单备注
     * @param supId 供应商id
     * @param session 用户session
     * @return 状态
     */
    @ApiOperation(value = "原材料增加", notes = "参数不能为空", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("insert")
    public ResponseMessage insert(@ApiParam(name = "arr", value = "原材料集合json", required = true) String arr, @ApiParam(name = "remark", value = "采购单备注", required = true) String remark, @ApiParam(name = "supId", value = "供应商Id", required = true) String supId, HttpSession session) {
        int insert = hqPurchaseorderService.insert(arr, remark, supId, session);
        return insert > 0 ? ResponseMessage.success("增加成功") : ResponseMessage.error("增加失败");
    }

    /**
     * 根据采购单id删除
     * @param purchaseId 采购单id
     * @return 状态
     */
    @ApiOperation(value = "采购订单管理 删除", notes = "根据采购单id删除", httpMethod = "POST", response = ResponseMessage.class)
    @DeleteMapping("deletes")
    @ResponseBody
    public ResponseMessage deletes(@ApiParam(name = "purchaseId", value = "采购单id", required = true) String purchaseId) {
        boolean b = hqPurchaseorderService.deleteById(purchaseId);
        return b ? ResponseMessage.success("删除成功") : ResponseMessage.error("删除失败");
    }

    /**
     * 根据采购id 订单原材料
     * @param vo 采购单参数
     * @return 原材料列表
     */
    @ApiOperation(value = "根据采购id 订单原材料", notes = "根据采购单id查询", httpMethod = "POST", response = Map.class)
    @PostMapping("findById")
    public Map<String, Object> findById(PurchaseMaterialVo vo) {
        List<PurchaseMaterialVo> list = hqPurchaseorderService.selectMaterial(vo);
        PurchaseMaterialVo p = hqPurchaseorderService.selectPurchaseRemarkSupId(vo);
        Map<String, Object> map = new HashMap<>(0);
        map.put("data", list);
        map.put("count", 0);
        map.put("msg", "");
        map.put("code", 0);
        map.put("remark", HtmlUtil.escape(p.getRemark()));
        map.put("supId", p.getSupId());
        return map;
    }

    /**
     * 根据采购id 修改订单
     * @param list 原材料集合json
     * @param remark 采购单备注
     * @param supName 供应商id
     * @param purchaseId 采购单id
     * @param session 用户session
     * @return 状态
     */
    @ApiOperation(value = "根据采购id 修改订单", notes = "根据采购单id修改", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("update")
    public ResponseMessage update(@ApiParam(name = "list", value = "原材料集合json", required = true) String list, @ApiParam(name = "remark", value = "采购单备注", required = true) String remark, @ApiParam(name = "supId", value = "供应商id", required = true) String supName, @ApiParam(name = "purchaseId", value = "采购单id", required = true) String purchaseId, HttpSession session) {
        boolean b = hqPurchaseorderService.updatePurchaseorder(list, remark, supName, purchaseId, session);
        return b ? ResponseMessage.success("修改成功") : ResponseMessage.error("修改失败");
    }

    /**
     * 根据采购单id修改状态
     * @param purchaseId 采购单id
     * @return 状态
     */
    @ApiOperation(value = "提交总经理审核", notes = "根据采购单id修改状态", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("updateisva")
    public ResponseMessage updateisva(@ApiParam(name = "purchaseId", value = "采购单id", required = true) String purchaseId) {
        boolean b = hqPurchaseorderService.updateisva(purchaseId, AuditStatus.YES_SQ.status);
        return b ? ResponseMessage.success() : ResponseMessage.error();
    }

    /**
     * 根据采购单id修改状态
     * @param purchaseId 采购单id
     * @return 状态
     */
    @ApiOperation(value = "提交财务审核", notes = "根据采购单id修改状态", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("updateisvas")
    public ResponseMessage updateisvas(@ApiParam(name = "purchaseId", value = "采购单id", required = true) String purchaseId) {
        boolean b = hqPurchaseorderService.updateisva(purchaseId, AuditStatus.YES_TJCWSQ.status);
        return b ? ResponseMessage.success() : ResponseMessage.error();
    }

    /**
     * 根据采购单id 查询信息加入到库存
     * @param purchaseId 采购单id
     * @param session 用户session
     * @return 状态
     */
    @ApiOperation(value = "加入库存", notes = "根据采购单id 查询信息加入到库存", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("joinTheInventory")
    public ResponseMessage insertJoinTheInventory(@ApiParam(name = "purchaseId", value = "采购单id", required = true) String purchaseId, HttpSession session) {
        boolean b = hqPurchaseorderService.insertJoinTheInventory(purchaseId, session);
        return b ? ResponseMessage.success() : ResponseMessage.error();
    }

}